package com.mm.common.exception;



import com.mm.common.domain.po.Result;
import com.mm.common.enums.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @BelongsProject: 0.forum_system
 * @BelongsPackage: com.example.forum_system.exception
 * @Author : 游在海里的鱼
 * @CreateTime : 2023-11-03  15:06
 * @Version : 1.0
 * @Description : 全局异常处理
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ResponseBody
    @ExceptionHandler(AppException.class)
    public Result appExceptionHandler(AppException e){
//        log.error(e.getMessage());
        e.printStackTrace();
        if(e.getErrorResult() != null){
            return e.getErrorResult();
        }
            return Result.failed(ResultCode.ERROR_SERVICES);
    }

    @ResponseBody
    @ExceptionHandler(Exception.class)
    public Result exceptionHandler(Exception e){
        e.printStackTrace();
        log.error(e.getMessage());
        return Result.failed(ResultCode.ERROR_SERVICES);
    }
}
